package com.yvan.codeslave;

import com.google.common.collect.Lists;
import com.intellij.database.model.DasColumn;
import com.intellij.database.psi.DbColumn;
import com.intellij.database.psi.DbTable;
import com.intellij.database.util.DasUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.impl.source.xml.XmlFileImpl;
import com.yvan.platform.FileUtils;
import com.yvan.platform.JsonWapper;
import com.yvan.platform.YvanUtil;
import lombok.val;

import java.util.List;

public class PluginUtils {
    /**
     * 保存的相对路径
     */
    private static final String SAVE_PATH = ".idea/CodeSlave";
    private static final String SAVE_FILE_NAME = "setting.yml";

    public static DbTable dbTable;
    public static List<DbTable> dbTableList;
    public static DbColumn dbColumn;
    public static List<DbColumn> dbColumnList;
    public static XmlFileImpl moduleFile;

    public static Project getProject() {
        val projects = ProjectManager.getInstance().getOpenProjects();
        if (projects.length > 0) {
            return projects[0];
        }
        return ProjectManager.getInstance().getDefaultProject();
    }


    public static class State {
        public String author;
        public String basePath;
    }

    private static State state;

    public static synchronized State getState() {
        if (state == null) {
            val file = FileUtils.getResource(PluginUtils.getProject().getBasePath(), SAVE_PATH, SAVE_FILE_NAME);
            try {
                state = YvanUtil.readYaml(State.class, file);
            } catch (Exception e) {
                state = new State();
            }
        }
        return state;
    }

    public static void save(State thisState) {

        val content = YvanUtil.toYaml(thisState);
        val dir = FileUtils.getResource(PluginUtils.getProject().getBasePath(), SAVE_PATH);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Messages.showWarningDialog("保存失败，无法创建目录。", "错误");
                return;
            }
        }
        val file = FileUtils.getResource(PluginUtils.getProject().getBasePath(), SAVE_PATH, SAVE_FILE_NAME);
        FileUtils.write(content, file);

        state = thisState;

        // 同步刷新
        VirtualFileManager.getInstance().syncRefresh();
    }

    public static JsonWapper getSelectStructs() {
        val jw = new JsonWapper();

        for (val dbColumn : dbColumnList) {

            jw.asDic(dbColumn.getTable().getName(), dbColumn.getName())
                    .set("name", dbColumn.getName())
                    .set("comment", dbColumn.getComment())
                    .set("dataType", dbColumn.getDataType().getSpecification())
                    .set("pk", false);
        }

        for (val dbTable : dbTableList) {
            if (jw.contains(dbTable.getName())) {
                continue;
            }
            val columns = DasUtil.getColumns(dbTable);
            val pk = Lists.newArrayList(DasUtil.getPrimaryKey(dbTable).getColumnsRef().iterate());

            for (DasColumn dbColumn : columns) {
                val dic = jw.asDic(dbTable.getName(), dbColumn.getName());
                dic.set("name", dbColumn.getName())
                        .set("comment", dbColumn.getComment())
                        .set("dataType", dbColumn.getDataType().getSpecification());
                dic.set("pk", pk.contains(dbColumn.getName()));
            }
        }

        return jw;
    }
}
